package com.hxzy.controller.system;

import com.hxzy.common.core.domain.AjaxResult;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysMenu;
import com.hxzy.common.core.domain.model.LoginBody;
import com.hxzy.common.core.domain.model.LoginUser;
import com.hxzy.common.utils.SecurityUtils;
import com.hxzy.system.domain.vo.RouterVo;
import com.hxzy.system.service.ISysMenuService;
import com.hxzy.system.service.ISysRoleService;
import com.hxzy.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.scene.effect.SepiaTone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

@Api(tags = "登录API")
@RestController
@RequestMapping(value = "/api")
public class LoginController {


    @Autowired
    private ISysUserService  sysUserService;

    @Autowired
    private ISysRoleService  sysRoleService;

    @Autowired
    private ISysMenuService  sysMenuService;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public R  login( @RequestBody @Valid LoginBody loginBody){
      String jwtToken=this.sysUserService.login(loginBody);


      return R.okHasData(jwtToken);
    }

    @ApiOperation(value = "获取当前登录用户")
    @GetMapping(value = "/system/user/getInfo")
    public R getUserInfo(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //重新查询角色
        Set<String>   roles=this.sysRoleService.selectRolePermissionByUserId(loginUser.getUserId());


        //重新查询菜单权限
        Set<String>  permissions=new HashSet<>();

        //如果是管理员就是所有权限
        if(roles.contains("admin")){
            permissions.add("*:*:*");
        }else{
            permissions=this.sysMenuService.selectMenuPermsByUserId(loginUser.getUserId());
        }

        Map<String,Object> ajax=new HashMap<>();
        ajax.put("user",loginUser);
        ajax.put("roles", roles);
        ajax.put("permissions",permissions);

        return R.okHasData(ajax);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation(value = "获取当前用户路由菜单")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        //查询该用户拥有的菜单权限对象
        List<SysMenu> menus = this.sysMenuService.selectMenuTreeByUserId(userId);
        //再把这个对象转换成前端需要的路由信息
        /*
          {
                path: '/user',
                component: Layout,      //模板，只能返回字符串 'Layout'
                hidden: true,
                redirect: 'noredirect',
                children: [
                  {
                    path: 'profile',
                    component: () => import('@/views/system/user/profile/index'), 只能返回页面字符串/system/user/profile/index
                    name: 'Profile',
                    meta: { title: '个人中心', icon: 'user' }
                  }
                ]
              }
         */
        List<RouterVo>  routerMenu= this.sysMenuService.buildMenus(menus);
        return AjaxResult.success(routerMenu );
    }
}
